package com.feeyin.radar.msg.dao.impl;

import com.feeyin.radar.commons.DateUtils;
import com.feeyin.radar.commons.FinalUtils;
import com.feeyin.radar.commons.Page;
import com.feeyin.radar.commons.StringUtils;
import com.feeyin.radar.msg.dao.InternalMailboxDAO;
import com.feeyin.radar.msg.model.InternalMailbox;
import com.feeyin.radar.msg.model.WarningInfo;
import com.feeyin.radar.sys.dao.ManageSetDAO;
import com.feeyin.radar.sys.dao.UserDAO;
import com.feeyin.radar.sys.model.Users;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.orm.ibatis.SqlMapClientTemplate;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

public class InternalMailboxDAOImpl extends SqlMapClientDaoSupport
  implements InternalMailboxDAO
{
  public ManageSetDAO manageSetDAO;
  public UserDAO userDAO;

  public ManageSetDAO getManageSetDAO()
  {
    return this.manageSetDAO;
  }

  public void setManageSetDAO(ManageSetDAO manageSetDAO) {
    this.manageSetDAO = manageSetDAO;
  }

  public UserDAO getUserDAO() {
    return this.userDAO;
  }

  public void setUserDAO(UserDAO userDAO) {
    this.userDAO = userDAO;
  }

  public List getPageInternalMailboxsByDynamic(InternalMailbox internalMailbox, Page page)
  {
    if (internalMailbox == null) return null;
    if (page == null) return null;
    internalMailbox.setPageStartRow(page.getPageStartRow());
    internalMailbox.setPageEndRow(page.getPageEndRow());
    return getSqlMapClientTemplate().queryForList("getPageInternalMailboxsByDynamic", internalMailbox);
  }

  public Integer getPageInternalMailboxsCountByDynamic(InternalMailbox internalMailbox)
  {
    if (internalMailbox == null) return Integer.valueOf(0);
    return (Integer)getSqlMapClientTemplate().queryForObject("getPageInternalMailboxsCountByDynamic", internalMailbox);
  }

  public InternalMailbox getInternalMailById(Integer id) {
    if (id == null) return null;
    return (InternalMailbox)getSqlMapClientTemplate().queryForObject("getInternalMailById", id);
  }

  public InternalMailbox getInternalMailByCode(String code) {
    if (StringUtils.isEmpty(code)) return null;
    return (InternalMailbox)getSqlMapClientTemplate().queryForObject("getInternalMailByCode", code);
  }

  public boolean insertInternalMail(InternalMailbox internalMailbox) {
    if (internalMailbox == null) return false;
    Integer result = (Integer)getSqlMapClientTemplate().insert("insertInternalMail", internalMailbox);
    return result != null;
  }

  public void insertInternalMail(WarningInfo warningInfo, int sign)
  {
    if ((warningInfo != null) && (this.manageSetDAO.isWarningWay("internal"))) {
      String code = DateUtils.getDateByFormat("yyyyMMddHHmmss");
      InternalMailbox internalMailbox = new InternalMailbox();
      String month = DateUtils.getStringDateByFormat(warningInfo.getWarningDate(), "MM");
      String day = DateUtils.getStringDateByFormat(warningInfo.getWarningDate(), "dd");
      Integer userId = warningInfo.getSponsorId();
      String warningUserIds = warningInfo.getUserIds();

      internalMailbox.setAddresserId(Integer.valueOf(0));
      internalMailbox.setCode(code);
      internalMailbox.setCreateDate(new Date());
      internalMailbox.setIsDeleteInbox("0");
      internalMailbox.setIsDeleteOutbox("2");
      internalMailbox.setIsRead("0");
      internalMailbox.setRecipientsId(warningInfo.getWarningUserId());
      if (sign == 0) {
        internalMailbox.setSubject(FinalUtils.TIMEREMIND);
        internalMailbox.setContent(month + FinalUtils.MONTH + day + FinalUtils.DAYOFMONTH + FinalUtils.LINK + " " + warningInfo.getForeignName());
        insertInternalMail(internalMailbox);
      } else if (sign == 1) {
        internalMailbox.setSubject(FinalUtils.SYSTEMREMIND);
        internalMailbox.setContent(FinalUtils.AGINGCONTRACT + FinalUtils.ID + warningInfo.getForeignCode() + " " + warningInfo.getForeignName());
        insertInternalMail(internalMailbox);
      } else if (sign == 2) {
        internalMailbox.setSubject(FinalUtils.SYSTEMREMIND);
        internalMailbox.setContent(warningInfo.getForeignName() + FinalUtils.BYMOVE);
        insertInternalMail(internalMailbox);
      } else if (sign == 3) {
        internalMailbox.setSubject(FinalUtils.SYSTEMREMIND);
        internalMailbox.setContent(warningInfo.getForeignName() + " " + warningInfo.getAgingDay() + FinalUtils.DAY + FinalUtils.AGING);
        insertInternalMail(internalMailbox);
      } else if ((sign == 4) && (userId != null) && (!StringUtils.isEmpty(warningUserIds))) {
        List users = new ArrayList();
        users = this.userDAO.getAuditUsers(userId);
        if ((users == null) || (users.size() <= 0)) return;
        for (int i = 0; i < users.size(); i++) {
          Users user = new Users();
          user = (Users)users.get(i);
          if ((warningUserIds.indexOf(user.getId().toString()) != -1) && (userId.compareTo(user.getId()) != 0)) {
            internalMailbox.setRecipientsId(user.getId());
            internalMailbox.setSubject(FinalUtils.AUDITREMIND);
            internalMailbox.setContent(FinalUtils.AUDITCONTRACT + FinalUtils.ID + warningInfo.getForeignCode() + " " + warningInfo.getForeignName());
            insertInternalMail(internalMailbox);
          }
        }
      } else if ((sign == 5) && (userId != null) && (!StringUtils.isEmpty(warningUserIds))) {
        List users = new ArrayList();
        users = this.userDAO.getAuditUsers(userId);
        if ((users == null) || (users.size() <= 0)) return;
        for (int i = 0; i < users.size(); i++) {
          Users user = new Users();
          user = (Users)users.get(i);
          if ((warningUserIds.indexOf(user.getId().toString()) != -1) && (userId.compareTo(user.getId()) != 0)) {
            internalMailbox.setRecipientsId(user.getId());
            internalMailbox.setSubject(FinalUtils.AUDITREMIND);
            internalMailbox.setContent(FinalUtils.AUDITSALECOST + FinalUtils.ID + warningInfo.getForeignCode());
            insertInternalMail(internalMailbox);
          }
        }
      }
    }
  }

  public boolean updateInternalMailByDynamic(InternalMailbox internalMailbox) {
    if (internalMailbox == null) return false;
    Integer result = Integer.valueOf(getSqlMapClientTemplate().update("updateInternalMailByDynamic", internalMailbox));
    return result != null;
  }

  public boolean deleteInternalMailById(Integer id)
  {
    if (id == null) return false;
    Integer result = Integer.valueOf(getSqlMapClientTemplate().delete("deleteInternalMailById", id));
    return result != null;
  }

  public boolean deleteInternalMailByDynamic(InternalMailbox internalMailbox)
  {
    if (internalMailbox == null) return false;
    Integer result = Integer.valueOf(getSqlMapClientTemplate().delete("deleteInternalMailByDynamic", internalMailbox));
    return result != null;
  }
}